package frame;

import java.awt.BorderLayout;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import Client.MyClient;

import javax.swing.JTextField;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPasswordField;
import java.awt.Color;
import java.awt.EventQueue;

public class Login extends JDialog {
	private static MyClient myClient;
	private String uname;
	private String upassword;
	private final JPanel contentPanel = new JPanel();
	private JLabel lbl_start;
	private final JLabel font_start_2 = new JLabel("");
	private JTextField text_username;
	private JPasswordField text_password;

	public void setUname(String uname) {
		this.uname = uname;
	}

	public void setUpassword(String upassword) {
		this.upassword = upassword;
	}

	public String getUname() {
		return uname;	
	}

	public String getUpassword() {
		return upassword;
	}


	/**
	 * Create the dialog.
	 */

	public Login(MyClient myClient) {
		this.myClient = myClient;
		setBounds(100, 100, 815, 444);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			ImageIcon imageIconSource1 = new ImageIcon("photo\\123.jpg");
		    Image image1 = imageIconSource1.getImage();
		    Image tempImage1 = image1.getScaledInstance(1000,400,Image.SCALE_DEFAULT);
		    ImageIcon imageIcon1 = new ImageIcon(tempImage1);
		    
		    JLabel font_start = new JLabel("");
		    font_start.setIcon(new ImageIcon("photo\\username.png"));
		    font_start.setBounds(71, 35, 256, 84);
		    contentPanel.add(font_start);
		    font_start_2.setFont(new Font("宋体", Font.BOLD, 25));
		    font_start_2.setForeground(Color.WHITE);
		    
		   

		    font_start_2.setIcon(new ImageIcon("photo\\password.png"));
		    font_start_2.setBounds(71, 156, 256, 77);
		    
		    contentPanel.add(font_start_2);
			ImageIcon imageIconSource2 = new ImageIcon("photo\\back_font.png");
		    Image image2 = imageIconSource2.getImage();
		    Image tempImage2 = image2.getScaledInstance(500,300,Image.SCALE_DEFAULT);
		    ImageIcon imageIcon2 = new ImageIcon(tempImage2);
		}
		ImageIcon imageIconSource1 = new ImageIcon("photo\\input.png");
	    Image image1 = imageIconSource1.getImage();
	    Image tempImage1 = image1.getScaledInstance(300,70,Image.SCALE_DEFAULT);
	    ImageIcon imageIcon1 = new ImageIcon(tempImage1);
		
		text_username = new JTextField();
		text_username.setForeground(Color.WHITE);
		text_username.setBackground(Color.BLACK);
		text_username.setFont(new Font("宋体", Font.BOLD, 25));
		text_username.setBounds(379, 71, 202, 34);
		text_username.setOpaque(false);
		contentPanel.add(text_username);
		text_username.setColumns(10);
		
		text_password = new JPasswordField();
		text_password.setForeground(Color.WHITE);
		text_password.setFont(new Font("宋体", Font.BOLD, 25));
		text_password.setBounds(379, 168, 202, 34);
		text_password.setOpaque(false);
		contentPanel.add(text_password);
		JLabel lb1 = new JLabel("");
		lb1.setIcon(imageIcon1);
		lb1.setBounds(341, 147, 280, 70);
		contentPanel.add(lb1);
		
		JLabel lb2 = new JLabel("");
		lb2.setIcon(imageIcon1);
		lb2.setBounds(351, 51, 270, 70);
		contentPanel.add(lb2);
		
		JLabel lb_next = new JLabel("");
		lb_next.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				uname = text_username.getText();
				upassword = new String(text_password.getPassword());
				if(uname.equals("")||upassword.equals("")) {
					JOptionPane.showMessageDialog(Login.this, "登录失败,账号密码不能为空");
					return;
				}
				// System.out.println(uname);
				// System.out.println(upassword);
				// 发送登录请求
				myClient.Login(uname, upassword);
			}
			@Override
			public void mouseEntered(MouseEvent e) {
				lb_next.setIcon(new MScaleIcon("photo\\font_start2.png"));
			}
			@Override
			public void mouseExited(MouseEvent e) {
				lb_next.setIcon(new MScaleIcon("photo\\font_start1.png"));
			}
		});
		lb_next.setIcon(new ImageIcon("photo\\font_start1.png"));
		lb_next.setBounds(42, 259, 225, 54);
		contentPanel.add(lb_next);
		
		JLabel forget_pasword = new JLabel("忘记密码？");
		forget_pasword.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {//忘记密码的界面
				FindPassword frame=new FindPassword(myClient);
				frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
				frame.setVisible(true);

			}
			@Override
			public void mouseEntered(MouseEvent e) {
				forget_pasword.setForeground(Color.white);
			}
			@Override
			public void mouseExited(MouseEvent e) {
				forget_pasword.setForeground(Color.RED);
			}
		});
		
		JLabel lb_next_1 = new JLabel("");
		lb_next_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				Register frame = new Register(myClient);
				//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
				frame.setVisible(true);
			}
			@Override
			public void mouseEntered(MouseEvent e) {
				lb_next_1.setIcon(new MScaleIcon("photo\\register2.png"));
			}
			@Override
			public void mouseExited(MouseEvent e) {
				lb_next_1.setIcon(new MScaleIcon("photo\\register1.png"));
			}
		});
		lb_next_1.setIcon(new ImageIcon("photo\\register1.png"));
		lb_next_1.setBounds(396, 259, 225, 54);
		contentPanel.add(lb_next_1);
		forget_pasword.setForeground(Color.RED);
		forget_pasword.setFont(new Font("宋体", Font.BOLD, 20));
		forget_pasword.setBounds(645, 77, 138, 27);
		contentPanel.add(forget_pasword);
		lbl_start = new JLabel("");
		lbl_start.setIcon(new ImageIcon("photo\\123.jpg"));
		lbl_start.setBounds(0, 0, 798, 400);
		contentPanel.add(lbl_start);
		setLocationRelativeTo(null);
	}

	public void isLogin(boolean a) {
		if (a) {
			myClient.setPlayerString(uname);
			JOptionPane.showMessageDialog(Login.this, "登录成功");
			this.dispose();
		} else {
			JOptionPane.showMessageDialog(Login.this, "登录失败,请检查自己的账号密码");
		}
	}
}
